package guda.screenshot.frame;

import guda.screenshot.IconLoader;
import guda.screenshot.biz.LoginBiz;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by well on 2014/12/30.
 */
public class MainFrame implements ActionListener {
    JFrame jf;
    JLabel timeLabel ;
    final  LoginBiz loginBiz;
    public MainFrame( final LoginBiz loginBiz){
        this.loginBiz = loginBiz;
        jf = new JFrame("工作记录系统");
        jf.setIconImage(IconLoader.getIconImage());
        jf.setLayout(new GridLayout(5, 1));
        jf.add(new JPanel());
        JLabel tips = new JLabel("工作记录中...",JLabel.CENTER);
        jf.add(tips);
        JLabel taskTip = new JLabel("当前任务编号:" + loginBiz.getTaskId(),JLabel.CENTER);
        jf.add(taskTip);

        timeLabel = new JLabel("您已经工作0秒",JLabel.CENTER);
        jf.add(timeLabel);


        JPanel jp3 = new JPanel();
        JButton jb2 = new JButton("退出");
        jb2.addActionListener(this);
        jp3.add(jb2);
        jf.add(jp3);

        jf.setResizable(false);
        jf.setVisible(true);
        jf.setSize(300, 200);
        jf.setLocation(300, 200);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final long start = System.currentTimeMillis();

        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                long time  = System.currentTimeMillis() - start;
                long second = time/1000;
                long hour = second/3600;
                long t = second%3600;
                long miniute = t/60;
                long m_t = t%60;
                StringBuilder buf = new StringBuilder();
                buf.append("您已经工作:");
                if(hour>0){
                    buf.append(hour).append("小时");
                }
                if(miniute>0){
                    buf.append(miniute).append("分");
                }
                if(m_t>0){
                    buf.append(m_t).append("秒");
                }

                timeLabel.setText(buf.toString());
            }
        };

        ActionListener taskScreenPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
               loginBiz.post();
            	
            }
        };
        new Timer(1000, taskPerformer).start();
        new Timer(5000, taskScreenPerformer).start();
    }

    public void actionPerformed(ActionEvent e) {
        String comm = e.getActionCommand();
        if (comm.equals("退出")) {
            System.exit(0);
        }
    }
}
